package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.oss.core.OssClient;
import com.ruoyi.oss.entity.UploadResult;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysOssMapper;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.FamilyEnvironmentBo;
import com.ruoyi.gp.domain.vo.FamilyEnvironmentVo;
import com.ruoyi.gp.domain.FamilyEnvironment;
import com.ruoyi.gp.mapper.FamilyEnvironmentMapper;
import com.ruoyi.gp.service.IFamilyEnvironmentService;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【家庭环境】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-04
 */
@RequiredArgsConstructor
@Service
public class FamilyEnvironmentServiceImpl implements IFamilyEnvironmentService {

    private final FamilyEnvironmentMapper baseMapper;

    private final ISysOssService iSysOssService;

    /**
     * 查询【家庭环境】
     */
    @Override
    public FamilyEnvironmentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【家庭环境】列表
     */
    @Override
    public TableDataInfo<FamilyEnvironmentVo> queryPageList(FamilyEnvironmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FamilyEnvironment> lqw = buildQueryWrapper(bo);
        Page<FamilyEnvironmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【家庭环境】列表
     */
    @Override
    public List<FamilyEnvironmentVo> queryList(FamilyEnvironmentBo bo) {
        LambdaQueryWrapper<FamilyEnvironment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FamilyEnvironment> buildQueryWrapper(FamilyEnvironmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FamilyEnvironment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getText()), FamilyEnvironment::getText, bo.getText());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), FamilyEnvironment::getImage, bo.getImage());
        lqw.eq(bo.getPatientId() != null, FamilyEnvironment::getPatientId, bo.getPatientId());
        return lqw;
    }

    /**
     * 新增【家庭环境】
     */
    @Override
    public Boolean insertByBo(MultipartFile file,FamilyEnvironmentBo bo) {
        String upload = upload(file);
        FamilyEnvironment add = BeanUtil.toBean(bo, FamilyEnvironment.class);
        add.setImage(upload);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【家庭环境】
     */
    @Override
    public Boolean updateByBo(FamilyEnvironmentBo bo) {
        FamilyEnvironment update = BeanUtil.toBean(bo, FamilyEnvironment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FamilyEnvironment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【家庭环境】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    public String upload(MultipartFile file) {
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        OssClient storage = OssFactory.instance();
        UploadResult uploadResult;
        try {
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        return uploadResult.getUrl();
    }


}
